package com.jyw.system.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.jyw.common.exception.ServiceException;
import com.jyw.system.service.IWeChatAppletService;
import me.chanjar.weixin.common.bean.WxJsapiSignature;
import me.chanjar.weixin.common.bean.WxOAuth2UserInfo;
import me.chanjar.weixin.common.bean.oauth2.WxOAuth2AccessToken;
import me.chanjar.weixin.common.bean.result.WxMediaUploadResult;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;
import me.chanjar.weixin.mp.enums.WxMpApiUrl;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;

/**
 * @author Created by ZhangCheng on 2024/01/22
 */
@Service
public class WeChatAppletServiceImpl implements IWeChatAppletService {

    @Resource
    private WxMpService wxMpService;


    @Override
    public WxJsapiSignature getSign(String url) {
        try {
            WxJsapiSignature wxJsapiSignature = wxMpService.createJsapiSignature(url);
            return wxJsapiSignature;
        } catch (Exception e) {
            e.printStackTrace();
            throw new ServiceException("获取Jsapi签名异常");
        }
    }

    @Override
    public void sendTemplateMsg(WxMpTemplateMessage templateMessage) {
        try {
            String responseContent = wxMpService.post(WxMpApiUrl.TemplateMsg.MESSAGE_TEMPLATE_SEND, templateMessage.toJson());
            System.out.println(responseContent);
        } catch (Exception e) {
            throw new ServiceException("发送模板消息异常");
        }
    }

    @Override
    public WxOAuth2UserInfo getWxUserInfo(String code) {
        try {
            WxOAuth2AccessToken accessToken = wxMpService.getOAuth2Service().getAccessToken(code);
            WxOAuth2UserInfo user = wxMpService.getOAuth2Service().getUserInfo(accessToken, null);
            return user;
        } catch (Exception e) {
            throw new ServiceException("获取微信用户信息异常");
        }
    }

    @Override
    public WxMediaUploadResult uploadFile(MultipartFile file, String mediaType) {
        try {
            String fileType = ObjectUtil.isNotEmpty(file.getOriginalFilename()) ? StrUtil.subAfter(file.getOriginalFilename(),
                    StrUtil.DOT, true) : null;
            WxMediaUploadResult result = wxMpService.getMaterialService().mediaUpload(mediaType, fileType, file.getInputStream());
            return result;
        } catch (Exception e) {
            throw new ServiceException("微信公众号上传文件异常");
        }
    }

    @Override
    public File getFileByMediaId(String mediaId) {
        try {
            File file = wxMpService.getMaterialService().mediaDownload(mediaId);
            return file;
        } catch (Exception e) {
            throw new ServiceException("微信公众号获取下载文件异常");
        }
    }

    @Override
    public File getSoundByMediaId(String mediaId) {
        try {
            File file = wxMpService.getMaterialService().jssdkMediaDownload(mediaId);
            return file;
        } catch (Exception e) {
            e.printStackTrace();
            throw new ServiceException("微信公众号获取下载文件异常");
        }
    }
}
